//
//  MOAccountRelevanceViewController.m
//  MobileOffice
//
//  Created by 晓 on 2019/6/7.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOAccountRelevanceViewController.h"
#import "MOAccountRelevanceViewCell.h"

@interface MOAccountRelevanceViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
//title
@property (nonatomic, strong) NSArray *titleArr;
//value
@property (nonatomic, strong) NSMutableArray *valueArr;
//保存
@property (nonatomic, strong) UIButton *saveButton;
//第三方系统dic
@property (nonatomic, strong) NSDictionary *appDataDic;

@end

@implementation MOAccountRelevanceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

- (void)initView{
    self.title = @"账号关联";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:self.saveButton];
    [self.view addSubview:self.tableView];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *placeA = @[@"请选择",@"请输入",@"请输入"];
    MOAccountRelevanceViewCell *cell = [MOAccountRelevanceViewCell cellWithTableView:tableView andIndexPath:indexPath];
    if (indexPath.section==0) {
        cell.textField.userInteractionEnabled = NO;
        cell.arrowImageView.hidden = NO;
        CGFloat width = SCREEN_WIDTH - 34 - kWConversions(150) - 7;
        cell.textField.x = SCREEN_WIDTH - 17 - 7 - width - 5;
    }else{
        cell.textField.userInteractionEnabled = YES;
        cell.arrowImageView.hidden = YES;
        CGFloat width = SCREEN_WIDTH - 34 - kWConversions(150) - 7;
        cell.textField.x = SCREEN_WIDTH - 17 - width;
    }
    if (indexPath.section==2) {
        cell.textField.secureTextEntry = YES;
    }else{
        cell.textField.secureTextEntry = NO;
    }
    cell.titleLabel.text = self.titleArr[indexPath.section];
    cell.textField.placeholder = placeA[indexPath.section];
    cell.textField.text = self.valueArr[indexPath.section];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return 10;
    }
    else{
        return 1;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0001;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section!=0) {
        return;
    }
    SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];

    NSArray *tArr = @[@"综合办公",@"数字人事"];
    NSArray *valueArr = @[@"OA",@"SZRS"];
    pickview.numberArray = tArr;
    
    __weak typeof (self)weakSelf = self;
    pickview.clickIndexBlock = ^(NSInteger index) {
        [weakSelf.valueArr replaceObjectAtIndex:0 withObject:tArr[index]];
        [weakSelf.tableView reloadData];
        [weakSelf requestUseCode:valueArr[index]];
    };
    [WINDOW addSubview:pickview];
}

-(void)requestUseCode:(NSString *)code
{
    NSString *postUrl = [NSString stringWithFormat:@"%@?userId=%@&appCode=%@&authorization=%@",GETAPPRELUSER_URL,[MOUser sharedMOUser].userName,code,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"获取第三方系统账号信息:%@",responseObject);
        self.appDataDic = responseObject;
        if ([NSString passNullForString:responseObject[@"app_user_account"]].length>0) {
            [self.valueArr replaceObjectAtIndex:1 withObject:[NSString passNullForString:responseObject[@"app_user_account"]]];
        }else{
            [self.valueArr replaceObjectAtIndex:1 withObject:@""];
        }
        if ([NSString passNullForString:responseObject[@"app_user_paswd"]].length>0) {
            [self.valueArr replaceObjectAtIndex:2 withObject:[NSString passNullForString:responseObject[@"app_user_paswd"]]];
        }else{
            [self.valueArr replaceObjectAtIndex:2 withObject:@""];
        }
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -点击事件
- (void)clickSaveBtn
{
    MOAccountRelevanceViewCell *cell1 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
    MOAccountRelevanceViewCell *cell2 = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]];;
    NSString *v1 = cell1.textField.text;
    NSString *v2 = cell2.textField.text;
    
    if ([self.valueArr[0] isEqualToString:@""]) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请先选择系统" showInView:self.view];
        return;
    }
    
    if (v1.length==0) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请先输入账号" showInView:self.view];
        return;
    }
    if (v2.length==0) {
        [[ADShowMessage shareShowMessage] showTextOnly:@"请先输入密码" showInView:self.view];
        return;
    }
    
    NSArray *tArr = @[@"综合办公",@"数字人事"];
    NSArray *valueArr = @[@"OA",@"SZRS",@"JXGL"];
    NSInteger index = [tArr indexOfObject:self.valueArr[0]];
    
    NSDictionary *user = @{
                           @"sys_user_id":[MOUser sharedMOUser].userName,
                           @"app_code":valueArr[index],
                           @"app_name":@"",
                           @"app_user_account":v1,
                           @"app_user_paswd":v2,
                           @"id":[NSString passNullForString:self.appDataDic[@"id"]]
                           };
    
    NSString *postUrl = [NSString stringWithFormat:@"%@?authorization=%@",SAVEAPPRELUSER_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [[ADShowMessage shareShowMessage] showHUDAddedTo:self.view animated:YES];
    
    [PPNetworkHelper POST:postUrl parameters:user success:^(id responseObject) {
        NSLog(@"保存或更新第三方系统信息:%@",responseObject);
        
        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        
        [[ADShowMessage shareShowMessage] showTextOnly:responseObject[@"msg"] showInView:self.view];
        
//        if ([responseObject[@"status"] integerValue]==1) {
//            [self.navigationController popViewControllerAnimated:YES];
//        }
        
        [self updateRedis:valueArr[index]];
        
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage] hideHUDForView:self.view animated:YES];
        
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    

}

-(void)updateRedis:(NSString *)value
{
    NSString *getUrl = [NSString stringWithFormat:@"%@/%@?authorization=%@",SETAPPRELUSERTOREDIUS_URL,value,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:getUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"第三方系统信息更新redis:%@",responseObject);
        
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage] showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
}

#pragma mark - 懒加载
- (UITableView *)tableView{
    if (_tableView ==nil ) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = KBackGrayColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return  _tableView;
}

-(NSArray *)titleArr{
    if (_titleArr == nil) {
        _titleArr = @[@"选择系统",@"账号",@"密码"];
    }
    return _titleArr;
}

-(NSMutableArray *)valueArr
{
    if (_valueArr == nil) {
        _valueArr = [NSMutableArray array];
        [_valueArr addObject:@""];
        [_valueArr addObject:@""];
        [_valueArr addObject:@""];
    }
    return _valueArr;
}

- (UIButton *)saveButton{
    if (!_saveButton) {
        _saveButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 15)];
        [_saveButton addTarget:self action:@selector(clickSaveBtn) forControlEvents:UIControlEventTouchUpInside];
        [_saveButton setTitle:@"保存" forState:UIControlStateNormal];
        [_saveButton setTitleColor:KTabbarBlueColor forState:UIControlStateNormal];
        _saveButton.titleLabel.font = FONT_SIZE(14);
    }
    return _saveButton;
}

@end
